# Article Title: Female Representation in Legislative Committees and Perceptions of Legitimacy: Evidence from a Harmonized Experiment in Jordan, Morocco, and Tunisia
# Purpose: Code for Figure 1 (Panels a and b)


#Load packages------
library("tidyverse")
library("ggplot2")
library("countrycode")
library("ggthemes")
library("ggrepel")

#Figure 1 panel a
##Load WVS data for Figure 1 panel a-----
load("replication_data/WV6_Data_R_v20201117.rdata")
wvs6 <- WV6_Data_R_v20201117

##Clean WVS Data------
# Clean variable: Men are better politicians than women
# Recode values so that higher values more conservative
wvs6<- 
 wvs6 %>% 
 mutate(.,
        men_better_pol = 5-V51)

# Generate country names
wvs6$cntry_name <- countrycode(wvs6$V2, 
                               origin = "wvs", 
                               destination = "country.name")
wvs6$cntry_name_shrt <- countrycode(wvs6$V2, 
                                    origin = "wvs", 
                                    destination = "iso2c")

# Create indicators for mrc/tns/jrd
wvs6 <-
 wvs6 %>% 
 mutate(.,
        current_study = case_when(
         cntry_name == "Jordan" ~ 1,
         cntry_name == "Morocco" ~ 1,
         cntry_name == "Tunisia" ~ 1,
         cntry_name == "United States" ~ 2
        ),
        current_study =ifelse(current_study %in% 
                               c(1:2),current_study,0))



# Collapse data
wvs6_mean <- 
 wvs6 %>%  
 group_by(cntry_name) %>% 
 summarize(.,
           cntry_name_shrt =  first(cntry_name_shrt),
           current_study =  first(current_study),
           men_better_pol = mean(men_better_pol, na.rm = T),
           cntry_name = first(cntry_name)) %>% 
 mutate(.,
        cntry_name = ifelse(current_study %in% c(1:2),
                            cntry_name, NA))

##Plot Country Means------
ggplot(wvs6_mean, aes(x=reorder(cntry_name_shrt, 
                                men_better_pol), 
                      y=men_better_pol,
                      label = cntry_name,
                      # color = as.factor(current_study),
                      fill = as.factor(current_study),
                      alpha = 0.5)) + 
 geom_bar(stat = "identity")+
 geom_hline(yintercept = mean(wvs6_mean$men_better_pol),
            color = "dodgerblue4", 
            linetype = 2)+
 geom_text_repel(segment.color = 'transparent',
                 family= "Times"
 ) +
 scale_y_discrete(limit = c("Strongly \nDisagree",
                            "Disagree", "Agree", "Strongly \nAgree"))+
 labs(x= "Country",
      y = "Are Men Better Politicians than Women",
      # title = "Men Make Better Political Leaders",
      color = "",
      fill = "")+
 scale_fill_manual(values = c("gray80", "dodgerblue3", "firebrick3"))+
 # scale_color_manual(values = c("gray70", "dodgerblue4"))+
 theme_tufte()+
 theme(text = element_text(size = 12, family = "Times"),
       legend.position = "none",
       axis.text.x=element_blank(),
       axis.ticks.x=element_blank(),
       plot.caption = element_text(size = 10, family = "Times",hjust = -.02))



#Figure 1 panel b
## Load Vdem data for Figure 1 panel b------
vdem <- readRDS("replication_data/V-Dem-CY-Core-v12.rds")

## subest to recent year --------
vdem_2021 <- vdem %>% 
 filter(.,
        year == 2021) %>% 
 mutate(.,
        current_study = case_when(
         country_name == "Jordan" ~ 1,
         country_name == "Morocco" ~ 1,
         country_name == "Tunisia" ~ 1,
         country_name == "United States of America" ~ 2
        ),
        current_study =ifelse(current_study %in% 
                               c(1:2),current_study,0),
        cntry_name = ifelse(current_study %in% c(1:2),
                            country_name, NA))


#3 Plot Electoral democracy index (v2x_polyarchy) -----

ggplot(vdem_2021, aes(x=reorder(country_text_id, 
                                v2x_polyarchy), 
                      y=v2x_polyarchy,
                      # color = as.factor(current_study),
                      fill = as.factor(current_study),
                      label = cntry_name,
                      alpha = 0.5,)) + 
 geom_bar(stat = "identity")+
 geom_hline(yintercept = mean(vdem_2021$v2x_polyarchy),
            color = "dodgerblue4", 
            linetype = 2)+
 geom_text_repel(segment.color = 'transparent',
                 family= "Times") +
 labs(x= "Country",
      y = "V-Dem Electoral Democracy Score",
      # title = "Men Make Better Political Leaders",
      color = "",
      fill = "")+
 ylim(0,1)+
 scale_fill_manual(values = c("gray80", "dodgerblue3", "firebrick3"))+
 # scale_color_manual(values = c("gray70", "dodgerblue4"))+
 theme_tufte()+
 theme(text = element_text(size = 12, family = "Times"),
       legend.position = "none",
       plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
       axis.text.x=element_blank(),
       axis.ticks.x=element_blank())
